package com.hospital.workstation.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.hospital.common.utils.StringUtils;
import com.hospital.common.core.page.TableDataInfo;
import com.hospital.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.hospital.workstation.domain.bo.HisDocumentRetrievealBo;
import com.hospital.workstation.domain.vo.HisDocumentRetrievealVo;
import com.hospital.workstation.domain.HisDocumentRetrieveal;
import com.hospital.workstation.mapper.HisDocumentRetrievealMapper;
import com.hospital.workstation.service.IHisDocumentRetrievealService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 检验检查报告检索Service业务层处理
 *
 * @author whb
 * @date 2023-03-09
 */
@RequiredArgsConstructor
@Service
public class HisDocumentRetrievealServiceImpl implements IHisDocumentRetrievealService {

    private final HisDocumentRetrievealMapper baseMapper;

    /**
     * 查询检验检查报告检索
     */
    @Override
    public HisDocumentRetrievealVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询检验检查报告检索列表
     */
    @Override
    public TableDataInfo<HisDocumentRetrievealVo> queryPageList(HisDocumentRetrievealBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<HisDocumentRetrieveal> lqw = buildQueryWrapper(bo);
        Page<HisDocumentRetrievealVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询检验检查报告检索列表
     */
    @Override
    public List<HisDocumentRetrievealVo> queryList(HisDocumentRetrievealBo bo) {
        LambdaQueryWrapper<HisDocumentRetrieveal> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<HisDocumentRetrieveal> buildQueryWrapper(HisDocumentRetrievealBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HisDocumentRetrieveal> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getMedicalRecordNo() != null, HisDocumentRetrieveal::getMedicalRecordNo, bo.getMedicalRecordNo());
        lqw.eq(StringUtils.isNotBlank(bo.getReportType()), HisDocumentRetrieveal::getReportType, bo.getReportType());
        lqw.eq(StringUtils.isNotBlank(bo.getDocumentId()), HisDocumentRetrieveal::getDocumentId, bo.getDocumentId());
        lqw.eq(StringUtils.isNotBlank(bo.getDocumentPath()), HisDocumentRetrieveal::getDocumentPath, bo.getDocumentPath());
        lqw.eq(StringUtils.isNotBlank(bo.getDocumentTitle()), HisDocumentRetrieveal::getDocumentTitle, bo.getDocumentTitle());
        lqw.eq(StringUtils.isNotBlank(bo.getDocumentType()), HisDocumentRetrieveal::getDocumentType, bo.getDocumentType());
        lqw.eq(StringUtils.isNotBlank(bo.getUpdateLastUser()), HisDocumentRetrieveal::getUpdateLastUser, bo.getUpdateLastUser());
        lqw.eq(bo.getUpdateLastTime() != null, HisDocumentRetrieveal::getUpdateLastTime, bo.getUpdateLastTime());
        return lqw;
    }

    /**
     * 新增检验检查报告检索
     */
    @Override
    public Boolean insertByBo(HisDocumentRetrievealBo bo) {
        HisDocumentRetrieveal add = BeanUtil.toBean(bo, HisDocumentRetrieveal.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改检验检查报告检索
     */
    @Override
    public Boolean updateByBo(HisDocumentRetrievealBo bo) {
        HisDocumentRetrieveal update = BeanUtil.toBean(bo, HisDocumentRetrieveal.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(HisDocumentRetrieveal entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除检验检查报告检索
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
